// pages/shopping-mall/shopping-mall.js
var app = getApp();
var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    util: util,

    isIphone_x: false,

    goodsList: [],



    noData: false,

    colorSet: '',

    scroll_h: 480,
    content_item_w: 240,
    goods_img_wh: 80,
    infoView_w: 160
  },

  mendianId: '',

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '加载中',
      mask: 'true'
    })
    var tc = app.get_tc();

    var that = this;
    that.mendianId = options.mendianid ? options.mendianid : '';
    wx.getSystemInfo({
      success: function(res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        var contentWidth = res.windowWidth;
        that.setData({
          isIphone_x: isIphone_x,
          scroll_h: isIphone_x ? (res.windowHeight - 20) : res.windowHeight,
          content_item_w: (contentWidth - 12),
          infoView_w: (contentWidth - that.data.goods_img_wh - 17)
        });
      }
    })

    this.loadGoodsListData();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  loadGoodsListData: function() {
    var that = this;

    var params = {
      publickey: util.publiccode,
      ac: "topiclist",
      topictype: '4',
      orgid: util.orgid,
      postuid: that.mendianId,
      page: 1,
      num: 100
    };

    console.log("商店商品请求参数：", params);
    util.api({
      data: params,
      success: function(responseData) {
        console.log("商店商品数据:", responseData);
        var resData = responseData.data;
        if (resData.msg) {
          that.setData({
            goodsList: [],
          });
        } else {
          
          var len = resData.length;    
          for (var i = 0; i < len; i++) {
            resData[i].kucun = parseInt(resData[i].kucun); 
          }
          for (var i = 0; i < len; i++) {        
            for (var j = 0; i < len - 1 - i; i++) {            
              if (resData[j].kucun > resData[j + 1].kucun) { //相邻元素两两对比
                var temp = resData[j + 1]; //元素交换        
                resData[j + 1] = resData[j];                
                resData[j] = temp;            
              }        
            }    
          }
          
          that.setData({
            goodsList: resData,
          });
        }

        if (that.data.goodsList.length <= 0) {
          that.setData({
            noData: true
          });
        };

        wx.hideLoading();
      }
    });
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },


  goAtherPageRedirect: function(e) {
    var url = e.currentTarget.dataset.link;
    if (url) {
      wx.redirectTo({
        url: url
      });
    }
  },
  goAtherPage: function(e) {
    var url = e.currentTarget.dataset.link;
    if (url) {
      wx.navigateTo({
        url: url
      });
    }
  },

  //空事件，阻止事件上传到父视图
  emtyAction: function() {

  }

})